package net.hubt.itc.ui.utils;




import android.app.Activity;
import android.content.Context;
import android.widget.TableLayout;
import android.widget.TableRow.LayoutParams;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

import net.hubt.itc.ui.R;
/**
 * SAMPLE USAGE:
 *		CTable table=new CTable(this);
 *
 *		//fill 2 columns horizontally
		table.stretchColumn(0, true);
		table.stretchColumn(1, true);
		
		table.addRow(table.makeRow(new int[]{Gravity.RIGHT,Gravity.LEFT}, "c100","c200"));
		table.addRow(table.makeRow("c300","c400"));
		table.addRow(table.makeRow("c5100","c600"));
		//span 2 columns
		table.addRow(table.makeColSpanRow(2, "column spanning"));
		table.addRow(table.makeRow("c100","c200"));
		table.addRow(table.makeRow("c300","c400"));
		table.addRow(table.makeRow("c5100","c600"));
		setContentView(table.getLayout()); //container.setContentView
		
 * @author quanla
 *
 */
public class CTable {
	private static final int RIGHT_MARGIN_PX = 2;
	private static final int ROW_SPACE_PX = 2;
	private static final int PADDING_RIGHT = 4;
	private static final int PADDING_BOTTOM = 3;
	
	private static final int BREAK_LINE_COLOR=Color.parseColor("#323232");	
	private Context mCtx;
	private TableLayout mTableLayout;
	private LayoutParams mTableLayoutParams;
	
	public static final int DEFAULT_TEXT_COLOR=Color.parseColor("#ffffff");
	public static final int DEFAULT_TEXT_SHADOW_COLOR=Color.parseColor("#000000");

	public CTable(Context ctx) {
		this(ctx,android.widget.TableLayout.LayoutParams.FILL_PARENT,
				android.widget.TableLayout.LayoutParams.FILL_PARENT);
	}
	
	public CTable(Context ctx, int layoutWidth, int layoutHeight){
		mCtx = ctx;
		mTableLayout = new TableLayout(ctx);
		mTableLayoutParams = new LayoutParams(
				layoutWidth,
				layoutHeight);
		mTableLayout.setLayoutParams(mTableLayoutParams);
	}

	/**
	 * for container addView()
	 * @return
	 */
	public TableLayout getLayout() {
		return mTableLayout;
	}

	public void stretchColumn(int cIndex, boolean isStretch) {
		mTableLayout.setColumnStretchable(cIndex, isStretch);
		
	}

	/**
	 * add new row to the table layout
	 * @param row
	 */
	public void addRow(TableRow row) {
		mTableLayout.addView(row);
	}

	/**
	 * make row contains multiple cell , cell's text and centered text alignment
	 * @param cellData
	 * @return
	 */
	public TableRow makeRow(String... cellData) {
		LayoutParams layoutParams = new LayoutParams(
				android.widget.TableRow.LayoutParams.FILL_PARENT,
				android.widget.TableRow.LayoutParams.WRAP_CONTENT);

		// individual row
		TableRow row = new TableRow(mCtx);
		row.setBackgroundColor(Color.BLACK);
		row.setLayoutParams(layoutParams);
		// l,t,r,b
		row.setPadding(0, 0, 0, ROW_SPACE_PX); // border between rows
		for (String cellText : cellData) {
			row.addView(makeCell(cellText));
		}
		return row;
	}

	/**
	 * make row contains multiple cells with text and text alignment
	 * each cell[i] will be aligned by gravity[i] 
	 * @param gravity
	 * @param cellData
	 * @return
	 */
	public TableRow makeRow(int[] gravity, boolean[] isLastCellInRow,String... cellData) {
		/*
		LayoutParams layoutParams = new LayoutParams(
				android.widget.TableRow.LayoutParams.FILL_PARENT,
				android.widget.TableRow.LayoutParams.WRAP_CONTENT);

		// individual row
		TableRow row = new TableRow(mCtx);
		row.setBackgroundColor(Color.BLACK);
		row.setLayoutParams(layoutParams);
		// l,t,r,b
		row.setPadding(0, 0, 0, ROW_SPACE_PX); // border between rows
		int i=0;
		for (String cellText : cellData) {
			row.addView(makeCell(cellText, gravity[i]));
			i++;
		}
		return row;
		*/
		return makeRow(gravity,Color.DKGRAY,Typeface.DEFAULT,isLastCellInRow, cellData);
	}
	
	
	
	/**
	 * create row with customized font style, background color
	 * @param gravity
	 * @param bgCellColor
	 * @param tf
	 * @param cellData
	 * @return
	 */
	public TableRow makeRow(int[] gravity, int bgCellColor,Typeface tf,boolean[] isLastCellInRow,String... cellData) {
		LayoutParams layoutParams = new LayoutParams(
				android.widget.TableRow.LayoutParams.FILL_PARENT,
				android.widget.TableRow.LayoutParams.WRAP_CONTENT);

		// individual row
		TableRow row = new TableRow(mCtx);
		row.setBackgroundColor(Color.BLACK);
		row.setLayoutParams(layoutParams);
		// l,t,r,b
		row.setPadding(0, 0, 0, ROW_SPACE_PX); // border between rows
		int i=0;
		for (String cellText : cellData) {
			row.addView(makeCell(cellText, gravity[i],bgCellColor,tf,isLastCellInRow[i]));
			i++;
		}
		return row;
	}

	/**	
	 * @param colSpan cell's column span
	 * @param cellData
	 * @return
	 */
	public TableRow makeColSpanRow(int colSpan, String... cellData) {
		LayoutParams layoutParams = new LayoutParams(
				android.widget.TableRow.LayoutParams.FILL_PARENT,
				android.widget.TableRow.LayoutParams.WRAP_CONTENT);
		layoutParams.span = colSpan;
		// individual row
		TableRow row = new TableRow(mCtx);
		row.setBackgroundColor(Color.BLACK);
		row.setLayoutParams(layoutParams);
		// l,t,r,b
		row.setPadding(0, 0, 0, ROW_SPACE_PX); // border between rows
		for (String cellText : cellData) {
			row.addView(makeSpanCell(cellText, colSpan));
		}
		return row;
	}

	/**
	 * make row contains cells with text
	 * each cell[i] will be aligned by gravity[i] 
	 * @param gravity
	 * @param colSpan
	 * @param cellData
	 * @return
	 */
	public TableRow makeColSpanRow(int[] gravity, int colSpan, boolean[] isLastCellInRow,String... cellData) {
		LayoutParams layoutParams = new LayoutParams(
				android.widget.TableRow.LayoutParams.FILL_PARENT,
				android.widget.TableRow.LayoutParams.WRAP_CONTENT);
		layoutParams.span = colSpan;
		// individual row
		TableRow row = new TableRow(mCtx);
		row.setBackgroundColor(Color.BLACK);
		row.setLayoutParams(layoutParams);
		// l,t,r,b
		row.setPadding(0, 0, 0, ROW_SPACE_PX); // border between rows
		int i=0;
		for (String cellText : cellData) {
			row.addView(makeSpanCell(cellText, colSpan, gravity[i],isLastCellInRow[i]));
			i++;
		}
		return row;
	}
	
	/**
	 * make a cell span multiple columns
	 * @param text
	 * @param colSpan
	 * @param gravity cell's text alignment
	 * @return
	 */
	public LinearLayout makeSpanCell(String text, int colSpan, int gravity, boolean isLastCellInRow) {
		LayoutParams layoutParams = new LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
		if(isLastCellInRow)
			layoutParams.setMargins(0, 0, 0, 0);// 2px right margin
		else
			layoutParams.setMargins(0, 0, RIGHT_MARGIN_PX, 0);// 2px right margin
		
		layoutParams.span = colSpan;

		// layout for individual cell
		LinearLayout cellLayout = new LinearLayout(mCtx);
		cellLayout.setBackgroundColor(BREAK_LINE_COLOR);
		cellLayout.setLayoutParams(layoutParams);
		cellLayout.setGravity(gravity);				
		cellLayout.addView(makeTextView(mCtx, R.drawable.dark_blue_theme_table_cell, text, Typeface.DEFAULT_BOLD, gravity));
		return cellLayout;
	}
	
	
	
	/**
	 * make a cell span multiple columns
	 * @param text
	 * @param colSpan
	 * @param gravity cell's text alignment
	 * @return
	 */
	public LinearLayout makeSpanCellOld(String text, int colSpan, int gravity) {
		LayoutParams layoutParams = new LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
		layoutParams.setMargins(0, 0, RIGHT_MARGIN_PX, 0);// 2px right margin
		layoutParams.span = colSpan;

		// layout for individual cell
		LinearLayout cellLayout = new LinearLayout(mCtx);
		cellLayout.setBackgroundColor(BREAK_LINE_COLOR);
		cellLayout.setLayoutParams(layoutParams);
		cellLayout.setGravity(gravity);

		TextView tv = new TextView(mCtx);		
		tv.setText(text);
		tv.setPadding(0, 0, PADDING_RIGHT, PADDING_BOTTOM);
		tv.setTypeface(Typeface.DEFAULT_BOLD);
		cellLayout.addView(tv);
		return cellLayout;
	}


	/**
	 * make cell has centered text and span multiple columns
	 * @param text
	 * @param colSpan
	 * @return
	 */
	private LinearLayout makeSpanCell(String text, int colSpan) {
		return makeSpanCell(text, colSpan, Gravity.CENTER_HORIZONTAL,true);
	}
	

	/**
	 * create a cell with text alignment (gravity)
	 * @param text
	 * @param gravity text alignment
	 * @return
	 */
	public LinearLayout makeCell(String text, int gravity) {		
		return makeCell(text,gravity,Color.DKGRAY,Typeface.DEFAULT);
	}
	
	public LinearLayout makeCell(String text, int gravity,int bgColor,Typeface tf) {
		return makeCell(text,gravity,bgColor,tf,false);
	}
	
	public LinearLayout makeCell(String text, int gravity,int bgColor,Typeface tf, boolean isLastCellInRow) {
		LayoutParams layoutParams = new LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.FILL_PARENT);
		
		//no need right space for the last cell in the current row 
		if(isLastCellInRow)
			layoutParams.setMargins(0, 0, 0, 0);// 2px right margin
		else{
			// NEED right space if this cell is not the last cell in the current row
			layoutParams.setMargins(0, 0, RIGHT_MARGIN_PX, 0);// 2px right margin
		}

		// layout for individual cell
		LinearLayout cellLayout = new LinearLayout(mCtx);
		cellLayout.setBackgroundColor(bgColor);
		cellLayout.setLayoutParams(layoutParams);
		//dont need these commands because of R.drawable.blue_theme_table_cell
//		cellLayout.setGravity(gravity);
//		cellLayout.setBackgroundResource(R.drawable.blue_theme_table_cell);				
		//dont need this command because of R.drawable.blue_theme_table_cell
//		tv.setPadding(0, 0, PADDING_RIGHT, PADDING_BOTTOM);
		TextView tv=makeTextView(mCtx, R.drawable.blue_theme_table_cell, text, tf, gravity);
		cellLayout.addView(tv);
		return cellLayout;
	}
	
	protected TextView makeTextView(Context context,int themeIdResource,String text,Typeface tf,int gravity) {
		LayoutParams lpTv = new LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.FILL_PARENT);
		TextView tv = new TextView(context);		
		//have to make sure this text view will fill width and height
		tv.setLayoutParams(lpTv);
		tv.setText(text);
		tv.setTypeface(tf);
		// a <selector> xml file placed in <drawable> dir
		tv.setBackgroundResource(themeIdResource);
		// effective alignment
		tv.setGravity(gravity);
		tv.setTextColor(DEFAULT_TEXT_COLOR);
		tv.setShadowLayer(.5f, 1, 1, DEFAULT_TEXT_SHADOW_COLOR);
		return tv;
	}
	
	public LinearLayout makeCellOld(String text, int gravity,int bgColor,Typeface tf) {
		LayoutParams layoutParams = new LayoutParams(
				android.widget.LinearLayout.LayoutParams.FILL_PARENT,
				android.widget.LinearLayout.LayoutParams.FILL_PARENT);
		layoutParams.setMargins(0, 0, RIGHT_MARGIN_PX, 0);// 2px right margin

		// layout for individual cell
		LinearLayout cellLayout = new LinearLayout(mCtx);
		cellLayout.setBackgroundColor(bgColor);
		cellLayout.setLayoutParams(layoutParams);
		cellLayout.setGravity(gravity);
//		cellLayout.setBackgroundResource(R.drawable.blue_theme_textview);

		TextView tv = new TextView(mCtx);		
		//tv.setLayoutParams(layoutParams);
		tv.setText(text);
		tv.setPadding(0, 0, PADDING_RIGHT, PADDING_BOTTOM);
		tv.setTypeface(tf);
//		tv.setBackgroundResource(R.drawable.blue_theme_textview);	
		cellLayout.addView(tv);
		return cellLayout;
	}

	/**
	 * create cell with center text alignment
	 * @param text
	 * @return
	 */
	public LinearLayout makeCell(String text) {
		return makeCell(text, Gravity.CENTER_HORIZONTAL);
	}

	
}
